// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html


var Take6Model = require("Take6Model");

cc.Class({
    extends: cc.Component,

    properties: {

    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    onJoinRoomButtonPressed : function()
    {
        var editBox = this.node.getChildByName('joinRoomEditBox');
        var roomNum = parseInt(editBox.getComponent(cc.EditBox).string);

        if (roomNum < 9999 && roomNum > 1000)
        {
            Take6Model.playerJoinRoom(roomNum);
        }
        else
        {
            this.showNoRoomError();// 弹出说明 不存在此房间
        }
    },

    onExitButtonPressed : function()
    {
        this.node.removeFromParent();
    },

    showNoRoomError : function()
    {
        var errorNode = this.node.getChildByName('errorLabel');
        errorNode.opacity = 0;
        errorNode.active = true;

        errorNode.runAction(cc.sequence(cc.fadeIn(0.5), cc.delayTime(1), cc.fadeOut(0.3)));
    },

    onJoinRoomOK : function()
    {
        this.node.getChildByName('joinRoomButton').active = false;
        this.node.getChildByName('exitButton').active = false;

        this.node.getChildByName('joinRoomEditBox').active = false;
        var roomNum = this.node.getChildByName('joinRoomEditBox').getComponent(cc.EditBox).string;
        this.node.getChildByName('roomLabel').getComponent(cc.Label).string = roomNum;
        this.node.getChildByName('roomLabel').active = true;

        var waitingLabelNode = this.node.getChildByName('waitingLabel');
        waitingLabelNode.oppacity = 0;
        waitingLabelNode.active = true;

        var action1 = cc.fadeIn(0.5);
        var action2 = cc.delayTime(1.0);
        var action3 = cc.fadeOut(0.3);
        var action4 = cc.delayTime(1.0);
        var actionUnion = cc.repeatForever(cc.sequence(action1, action2, action3, action4));

        waitingLabelNode.runAction(actionUnion);
    },

    start () {

    },

    // update (dt) {},
});
